/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package columndiv;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Dovgopoliy Serjiy
 */
public class ColumnDiv {

    private static Scanner in = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        menu();
    }
    
    public static void menu(){
        Integer[] num = inputNumbers();
        if(isDivNeeded(num[0], num[1])){
            ArrayList<Integer[]> resMatrix = doDiv(num[0], num[1]);
            outPut(resMatrix, num[0], num[1]);
        }
        else{
            System.out.println(num[0] + "|_" + num[1]);
            System.out.println("result :" + 0 + ", rest = " + num[0]);
        }
        
    }
    /**
     * 
     * @return the array which consist of  inputed divisor and divident
     */
    private static Integer[] inputNumbers() {

        Integer[] num = new Integer[2];
        try {
            System.out.println("input the divisor");
            num[0] = in.nextInt();
            do {
                System.out.println("input the divident");
                num[1] = in.nextInt();
                if (num[1] == 0) {
                    System.out.println("The divident should not be 0. Reinput the divident.");
                }
            } while (num[1] == 0);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("The program will close");
            System.exit(0);
        }
        return num;
    }
    /**
     * 
     * @param n - integer which will be transform to ArrayList
     * @return ArrayList which was created from inputed integer
     */
    private static ArrayList<Integer> toList(Integer n) {
        String s = n.toString();
        String[] s1 = s.split("");
        ArrayList<Integer> arrNum = new ArrayList<Integer>();
        for (int i = 1; i < s1.length; i++) {
            arrNum.add(new Integer(s1[i]));
        }
        return arrNum;
    }

    /**
     * 
     * @param divisor
     * @param divident
     * @return ArrayList if Integer arrays which consist of div-steps-patr-values
     */
    private static ArrayList<Integer[]> doDiv(Integer divisor, Integer divident) {
        ArrayList<Integer> divList = toList(divisor);
        Integer[] dataOfPart;
        int tmp;
        ArrayList<Integer[]> res = new ArrayList<Integer[]>();
        if (!isDivNeeded(divisor, divident)) {
            Integer[] mas = {0, divisor};
            res.add(mas);
            return res;
        }
        tmp = divList.get(0);
        for (int i = 1;;) {
            dataOfPart = findEnough(i, tmp, divList, divident);
            if (dataOfPart[0] == 0) {
                return res;
            }
            tmp = dataOfPart[0];
            i = dataOfPart[1];
            System.out.println("i = " + i);
            Integer[] divRes = divInside(tmp, divident);
            res.add(divRes);
            tmp = divRes[1];
        }
    }

    /**
     * 
     * @param divisor
     * @param divident
     * @return boolean  
     */
    private static boolean isDivNeeded(Integer divisor, Integer divident) {
        if (divisor >= divident) {
            return true;
        }
        return false;
    }

    /**
     * @param tmp - temporary value
     * @param divident
     * @return Integer array which consist of integer div result, rest of div and temporary value
     */
    private static Integer[] divInside(int tmp, Integer divident) {
        Integer[] res = new Integer[3];
        res[0] = tmp / divident;
        res[1] = tmp % divident;
        res[2] = tmp;
        return res;
    }

    /**
     * 
     * @param index - current index
     * @param tmp - temporary value of divisor
     * @param divisor
     * @param divident
     * @return min number which can div to divident
     */
    private static Integer[] findEnough(int index, int tmp, ArrayList<Integer> divisor, Integer divident) {
        Integer[] res = {0, 0};
        if (index >= divisor.size()) {
            return res;
        }
        for (int i = index; i <= divisor.size(); i++) {
            System.out.println(i);
            if (tmp < divident && index != divisor.size()) {
                tmp = tmp * 10 + divisor.get(index);
            } else {
                res[0] = tmp;
                res[1] = i;
                return res;
            }
        }
        return null;
    }
    
    /**
     * 
     * @param divRes - arrayList of Integer arrays which is the result of div action
     * @param divisor
     * @param divident 
     */
    private static void outPut(ArrayList<Integer[]> divRes, Integer divisor, Integer divident) {
        int point = 0;
        System.out.println(divisor + "|_" + divident);
        
        for (int i = 0; i < divRes.size(); i++) {
            for (int pr = 0; pr < point; pr++) {
                System.out.print(" ");
            }
            System.out.print(divRes.get(i)[2]);
            System.out.println(); 
            
            for (int pr = 0; pr < point + divRes.get(i)[2].toString().length() - divRes.get(i)[1].toString().length(); pr++) {
                System.out.print(" ");
            }
            System.out.print(divident);
            System.out.println();
            
            for (int pr = 0; pr < point + divRes.get(i)[2].toString().length() - divRes.get(i)[1].toString().length(); pr++) {
                System.out.print(" ");
            }
            System.out.print(divRes.get(i)[1]);
            System.out.println();

            point = point + divRes.get(i)[2].toString().length();
        }
        System.out.println("result :" + divisor/divident + ", rest = " + divRes.get(divRes.size()-1)[1]);
    }
}
